package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.annottation.LogAnnottion;
import com.baizhi.cmfz.entity.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.apache.catalina.core.ApplicationContext;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/banner")
public class BannerController {
@Autowired
    private BannerService bannerService;

    /**
     * @RequiresRoles(value = "superadmin") 添加在方法上  校验角色 没有对应的角色不能访问该方法
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selectByPageBanner")
    @ResponseBody
    @LogAnnottion(type = "select",value = "查询所有轮播图")
    @AddCacheAnnotation
    @RequiresRoles(value = "banner:create")
    public Map selectByPageBanner(Integer page,Integer limit){
        return  bannerService.selectByPageBanner(page,limit);
    }

    /**
     * 添加方法上 校验权限 没有对应的权限不能访问该方法
     * @param file
     * @param banner
     * @param request
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/insertBanner")
    @LogAnnottion(type = "select",value = "添加轮播图")
    @DeleteCacheAnnotation
    @RequiresRoles(value = "superadmin")
    public  Map insertBanner(MultipartFile file, Banner banner, HttpServletRequest request) throws IOException {

        if(file !=null){



            System.out.println("-------------"+file.getOriginalFilename());
            //  System.out.println("-------duix -------"+banner);
            //获取img文件夹在服务器磁盘的绝对路径
            String realPath = request.getServletContext().getRealPath("/img");
            System.out.println(realPath);
            //生成不重复的字符串
            String uuid = UUID.randomUUID().toString();
            //拼接原来的文件名
            String newFileName = uuid+"_"+file.getOriginalFilename();
            System.out.println(realPath+"/"+newFileName);
            //将文件写入到服务器磁盘
            file.transferTo(new File(realPath+"/"+newFileName));
            banner.setBannerImage(newFileName);
            banner.setBannerDate(new Date());
            System.out.println(banner+"======海报=======");
//        return  bannerService.insertBanner(banner);
            //request.getServletContext();
        }


        return  bannerService.insertBanner(banner);
    }
    @RequestMapping("/deleteBanner")
    @ResponseBody
    @LogAnnottion(type = "delete", value = "删除一个轮播图")
    @DeleteCacheAnnotation
    @RequiresRoles(value = "superadmin")
    public  Map deleteBanner(Integer bannerId){

        return  bannerService.deleteBanner(bannerId);

    }
    @ResponseBody
    @RequestMapping("/updateBanner")
    @LogAnnottion(type = "update", value = "修改轮播图")
    @DeleteCacheAnnotation
    @RequiresRoles(value = "superadmin")
    public  Map updateBanner(Banner banner){
        System.out.println(banner+"=====修改");

        return  bannerService.updateBanner(banner);
    }

    @RequestMapping("selectOneBanner")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询一个轮播图")
    @DeleteCacheAnnotation
    @RequiresRoles(value = "superadmin")
    public  Banner selectOneBanner(Integer id){
        return  bannerService.selectOneBanner(id);
    }

    @ResponseBody
    @RequestMapping("/updateState")
    @LogAnnottion(type = "update",value = "修改轮播图")
    @DeleteCacheAnnotation
    @RequiresRoles(value = "superadmin")
    public  Map updateState(Integer id,Integer state){

        return  bannerService.updateState(id,state);
    }
}
